from xml.dom.minidom import Document
import webapp2
from google.appengine.ext import ndb
import data
from google.appengine.datastore.datastore_query import Cursor

xmlns = 'https://code.google.com/p/cloudreader/'

class AjaxHandler(webapp2.RequestHandler):
    def get(self):
        doc = Document()
        rdr = doc.createElement('rdr')
        rdr.setAttribute('xmlns', xmlns)

        if self.request.get('sublist') == 'yes':
            sublist = doc.createElement('sublist')

            q = data.Subscription.query()\
                    .filter(data.Subscription.enabled == True)
            for sub in q:
                sub_e = doc.createElement('sub')

                text = doc.createTextNode(sub.key.id())
                e = doc.createElement('key')
                e.appendChild(text)
                sub_e.appendChild(e)

                text = doc.createTextNode(sub.name)
                e = doc.createElement('name')
                e.appendChild(text)
                sub_e.appendChild(e)

                text = doc.createTextNode(str(sub.count))
                e = doc.createElement('count')
                e.appendChild(text)
                sub_e.appendChild(e)

                sublist.appendChild(sub_e)

            rdr.appendChild(sublist)

        sub = self.request.get('sub')
        if sub:
            showall = int(self.request.get('showall', default_value='0'))
            cursor = self.request.get('cursor', default_value=None)

            q = data.Item.query()
            if sub != 'all':
                key = ndb.Key('Subscription', sub)
                q = q.filter(data.Item.sub == key)
            if not showall:
                q = q.filter(data.Item.unread == True)

            q = q.order(-data.Item.time)
            if cursor:
                curs = Cursor(urlsafe=cursor)
            else:
                curs = None

            if curs is not None:
                items, new_curs, more = q.fetch_page(30, batch_size=30,
                                                     start_cursor=curs)
            else:
                items, new_curs, more = q.fetch_page(30, batch_size=30)
            items_e = doc.createElement('itemlist')
            items_e.setAttribute('more', str(more))
            if more and new_curs:
                items_e.setAttribute('cursor', new_curs.urlsafe())

            for item in items:
                item_e = doc.createElement('item')

                text = doc.createTextNode(item.title)
                e = doc.createElement('title')
                e.appendChild(text)
                item_e.appendChild(e)

                text = doc.createTextNode(item.sub.id())
                e = doc.createElement('sub')
                e.appendChild(text)
                item_e.appendChild(e)

                text = doc.createTextNode(item.link)
                e = doc.createElement('link')
                e.appendChild(text)
                item_e.appendChild(e)

                text = doc.createTextNode(item.description)
                e = doc.createElement('description')
                e.appendChild(text)
                item_e.appendChild(e)

                text = doc.createTextNode(str(item.time))
                e = doc.createElement('time')
                e.appendChild(text)
                item_e.appendChild(e)

                text = doc.createTextNode(str(item.unread))
                e = doc.createElement('unread')
                e.appendChild(text)
                item_e.appendChild(e)

                text = doc.createTextNode(str(item.starred))
                e = doc.createElement('starred')
                e.appendChild(text)
                item_e.appendChild(e)

                text = doc.createTextNode(item.key.id())
                e = doc.createElement('guid')
                e.appendChild(text)
                item_e.appendChild(e)

                items_e.appendChild(item_e)

            rdr.appendChild(items_e)
        doc.appendChild(rdr)
        self.response.headers['Content-Type'] = 'application/xml'
        self.response.out.write(doc.toxml('utf-8'))


app = webapp2.WSGIApplication([('/ajax', AjaxHandler)], debug=True)
